package net.muchine.web.front.controller;

import java.util.Locale;

import javax.validation.Valid;

import net.muchine.core.domain.user.Preference;
import net.muchine.core.domain.user.User;
import net.muchine.core.service.user.PreferenceService;
import net.muchine.core.service.user.UserService;
import net.muchine.web.common.Messages;
import net.muchine.web.common.Pages;
import net.muchine.web.front.forms.Account;
import net.muchine.web.front.forms.Password;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/my")
@SessionAttributes("account")
public class MyController extends BaseController {
	@Autowired private UserService userService;
	@Autowired private PreferenceService prefService;
	
	private static final String MODEL_ACCOUNT	= "account";
	private static final String MODEL_PASSWORD	= "password";
	
	@RequestMapping(value="/account")
	public String goAccount(ModelMap model) {
		User user = userService.find(session.getSeq());
		Preference preference = prefService.findByUserSeq(session.getSeq());
		
		Account account = new Account();
		account.setUser(user);
		account.setPreference(preference);
		
		model.put(MODEL_ACCOUNT, account);
		this.setAccountCodeLists(model);
		return Pages.MY_ACCOUNT;
	}
	
	@RequestMapping(value="/account", method=RequestMethod.POST)
	public String updateAccount(
			   @ModelAttribute @Valid Account account
			 , BindingResult result
			 , SessionStatus status
			 , ModelMap model
			 , Locale locale) {
		
		if (result.hasErrors()) {
			this.logErrors(result);
						
			this.setAccountCodeLists(model);
			return Pages.MY_ACCOUNT;
		}
		
		log.info("update user account and preferences... id: " + account.getUser().getId());
	
		userService.update(account.getUser());
		prefService.update(account.getPreference());
		
		// update user session
		session.initialize(account.getUser(), account.getPreference());
		
//		status.setComplete();
		model.put(Messages.NOTICE, 
				this.getResourceBundle(locale).getString("Success.update.account"));
		return this.goAccount(model);
	}
	
	@RequestMapping(value="/password")
	public String goPassword(ModelMap model) {
		model.put(MODEL_PASSWORD, new Password());
		return Pages.MY_PASSWORD;
	}
	
	@RequestMapping(value="/password", method=RequestMethod.POST)
	public String updatePassword(
			  @ModelAttribute @Valid Password password
			, BindingResult result
			, ModelMap model
			, Locale locale) {
		User user = userService.find(session.getSeq());
		if (!user.getPassword().equals(password.getPassword())) {
			model.put(Messages.ERROR, 
					this.getResourceBundle(locale).getString("Incorrect.password"));
			return Pages.MY_PASSWORD;
		}
		
		if (result.hasErrors()) {
			this.logErrors(result);
			return Pages.MY_PASSWORD;	
		}
		
		// Update user password
		user.setPassword(password.getNewPassword());
		userService.update(user);
		
		model.put(Messages.NOTICE,
				this.getResourceBundle(locale).getString("Success.update.password"));
		
		return this.goAccount(model);
	}
}
